package com.xijing.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.xijing.model.*;
import com.xijing.service.LoginService;


public class LoginFrame extends JFrame implements ActionListener, KeyListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextField tf_id = null;
	private JLabel jl_id = null;
	private JLabel jl_pasword = null;
	private JButton btn_login = null;
	private JLabel jl_title = null;
	private JLabel lerror = null;
	private JPasswordField tf_password = null;


	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		this.setBounds(new Rectangle(300, 80, 600, 400));
		this.setContentPane(getJContentPane());
		this.setTitle("登陆界面");
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_login) {
			loginAction();
		}
	}

	private void loginAction() {
		lerror.setText("");
		if (tf_id.getText().equalsIgnoreCase("")) {
			lerror.setText("管理员ID不能为空");
		} else if (tf_password.getPassword().length <= 0) {
			lerror.setText("密码不能为空");
		} else {
			//调用登录服务
			LoginService loginService = new LoginService();
			
			SYS_User dto = new SYS_User();
			dto.setSU_LoginName(tf_id.getText());
			dto.setSU_Password(new String(tf_password.getPassword()));
			
			Map<String, Object> rsMap = loginService.login(dto);
			if(rsMap!=null) {
				boolean succ = Boolean.parseBoolean(rsMap.get("succ").toString());
				if(succ) {
					//JOptionPane.showMessageDialog(this, "登录成功");
					//这里就开始画面跳转
					MainFrame MainF=new MainFrame();
					MainF.setVisible(true);
					this.dispose();
					//...
				} else {
					lerror.setText(rsMap.get("msg").toString());
				}
			}
			
		}
	}
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lerror = new JLabel();
			lerror.setBounds(new Rectangle(96, 278, 249, 48));
			lerror.setFont(new Font("Dialog", Font.BOLD, 18));
			lerror.setForeground(Color.red);
			lerror.setText("");
			jl_title = new JLabel();
			jl_title.setBounds(new Rectangle(173, 49, 270, 50));
			jl_title.setFont(new Font("Dialog", Font.BOLD, 36));
			jl_title.setHorizontalAlignment(SwingConstants.CENTER);
			jl_title.setText("宾馆管理系统");
			jl_pasword = new JLabel();
			jl_pasword.setBounds(new Rectangle(74, 214, 108, 24));
			jl_pasword.setFont(new Font("Dialog", Font.PLAIN, 18));
			jl_pasword.setText("管理员密码：");
			jl_id = new JLabel();
			jl_id.setBounds(new Rectangle(74, 153, 108, 24));
			jl_id.setFont(new Font("Dialog", Font.PLAIN, 18));
			jl_id.setText("管理员编号：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getTf_id(), null);
			jContentPane.add(jl_id, null);
			jContentPane.add(jl_pasword, null);
			jContentPane.add(getBtn_login(), null);
			jContentPane.add(jl_title, null);
			jContentPane.add(lerror, null);
			jContentPane.add(getTf_password(), null);
		}
		return jContentPane;
	}
	
	private JTextField getTf_id() {
		if (tf_id == null) {
			tf_id = new JTextField();
			tf_id.setBounds(new Rectangle(200, 150, 288, 35));
			tf_id.setFont(new Font("Dialog", Font.PLAIN, 18));
			tf_id.addKeyListener(this);
		}
		return tf_id;
	}

	/**
	 * This method initializes tf_password
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getTf_password() {
		if (tf_password == null) {
			tf_password = new JPasswordField();
			tf_password.setBounds(new Rectangle(199, 214, 289, 32));
			tf_password.setFont(new Font("Dialog", Font.PLAIN, 18));
			tf_password.addKeyListener(this);
		}
		return tf_password;
	}
	
	/**
	 * This method initializes btn_login
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtn_login() {
		if (btn_login == null) {
			btn_login = new JButton();
			btn_login.setBounds(new Rectangle(358, 274, 127, 58));
			btn_login.setFont(new Font("Dialog", Font.BOLD, 25));
			btn_login.setText("登  陆");
			btn_login.addActionListener(this);
		}
		return btn_login;
	}
	

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() == KeyEvent.VK_ENTER) {
			loginAction();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

}
